
using System;
using System.Linq;
using Csla;
using Csla.Data;

namespace HousingInfo.Library
{
    public partial class NewsBussinessBOList : Csla.BusinessListBase<NewsBussinessBOList, NewsBussinessBO>
    {
        #region ham lay tat ca danh sach tin doanh nghiep
        public static NewsBussinessBOList GetAllNewsBussinessBOList()
        {
            return DataPortal.Fetch<NewsBussinessBOList>();
        }
        #endregion
        #region Authorization Rules

        protected static void AddObjectAuthorizationRules()
        {
            //TODO: Define object-level authorization rules of NewsBussinessBOList
            //AuthorizationRules.AllowCreate(typeof(NewsBussinessBOList), "NewsBussinessBOListCreateGroup"));
            //AuthorizationRules.AllowEdit(typeof(NewsBussinessBOList), "NewsBussinessBOListEditGroup"));
            //AuthorizationRules.AllowDelete(typeof(NewsBussinessBOList), "NewsBussinessBOListDeleteGroup"));
        }
        #endregion //Authorization Rules
        #region truy cap du lieu cua ham lay tat ca danh sach tin doanh nghiep
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch()
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.NewsBussinesses
                    orderby row.PostDate descending
                    select NewsBussinessBO.GetNewsBussinessBO(row.NewsBussinessID)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #region Ham lay 5 tin doanh nghiep moi nhat
        #region Ham
        public static NewsBussinessBOList GetNewsBussinessBOList_Random_ByNumber(int number)
        {
            return DataPortal.Fetch<NewsBussinessBOList>(new FilterCriteriaGetNewsBussiness_Random_ByNumber(number));
        }
        #endregion //End ham lay tung loai danh muc

        #region khai bao tham so
        private class FilterCriteriaGetNewsBussiness_Random_ByNumber
        {
            public int soluong;
            public FilterCriteriaGetNewsBussiness_Random_ByNumber(int sl)
            {
                this.soluong = sl;
            }
        }
        #endregion//end khai bao tham so

        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaGetNewsBussiness_Random_ByNumber criteria)
        {
            bool cancel = false;
            //OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (criteria.soluong == -1)
                {
                    this.AddRange(
                        from row in mgr.DataContext.NewsBussinesses
                        where row.IsActive == true && row.ExpireDate >= DateTime.Now
                        // orderby new Random().Next()
                        orderby row.PostDate
                        select NewsBussinessBO.GetNewsBussinessBO(row.NewsBussinessID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in mgr.DataContext.NewsBussinesses
                         where row.IsActive == true && row.ExpireDate >= DateTime.Now
                         // orderby new Random().Next()
                         orderby row.PostDate
                         select NewsBussinessBO.GetNewsBussinessBO(row.NewsBussinessID)).Take(criteria.soluong)
                    );
                }

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion//end truy cap

        #endregion //End lay random cac danh muc


        #region Trang Quan ly tin Doanh Nghiep
        #region ham lay tat ca danh tin chua kich hoat - het han - moi gia han
        public static NewsBussinessBOList GetAllNewsBussinessList_ByNotActive_Exprided(bool hethan, bool chuakichhoat, bool giahan)
        {
            return DataPortal.Fetch<NewsBussinessBOList>(new FilterCriteriaNewsBussinessList_ByNotActive_Exprided(hethan, chuakichhoat, giahan));
        }
        #endregion
        //khai bao tham so
        #region Khai bao tham so
        private class FilterCriteriaNewsBussinessList_ByNotActive_Exprided
        {
            public bool ishethan;
            public bool chuakichhoat;
            public bool isgiahan;
            public FilterCriteriaNewsBussinessList_ByNotActive_Exprided(bool hh, bool ckh, bool gh)
            {
                this.ishethan = hh;
                this.chuakichhoat = ckh;
                this.isgiahan = gh;
            }
        }
        #endregion
        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaNewsBussinessList_ByNotActive_Exprided param)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (param.ishethan == true)
                {
                    //tin het han
                    this.AddRange(
                        from row in mgr.DataContext.NewsBussinesses
                        where row.ExpireDate < DateTime.Now
                        orderby row.PostDate descending
                        select NewsBussinessBO.GetNewsBussinessBO(row.NewsBussinessID)
                    );
                }
                else if (param.chuakichhoat == true)
                {
                    //moi dang hoac sua
                    this.AddRange(
                        from row in mgr.DataContext.NewsBussinesses
                        where row.IsActive == false && row.IsExtension == false && row.ExpireDate >= DateTime.Now
                        orderby row.PostDate descending
                        select NewsBussinessBO.GetNewsBussinessBO(row.NewsBussinessID)
                    );
                }
                else
                {
                    //gia han
                    this.AddRange(
                        from row in mgr.DataContext.NewsBussinesses
                        where row.IsActive == false && row.IsExtension == true && row.ExpireDate >= DateTime.Now
                        orderby row.PostDate descending
                        select NewsBussinessBO.GetNewsBussinessBO(row.NewsBussinessID)
                    );
                }

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region kiem tra het han cua tin - neu het han thi kich hoat = false
        public static void KiemTra_TinDN_HetHan()
        {
            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                NewsBussinessBO newsbussinessBo;
                var result = from row in mgr.DataContext.NewsBussinesses
                             where row.IsActive == true
                             select row;
                foreach (var item in result)
                {
                    newsbussinessBo = NewsBussinessBO.GetNewsBussinessBO(item.NewsBussinessID);
                    if (newsbussinessBo.ExpireDate < DateTime.Now)
                    {
                        newsbussinessBo.IsActive = false;
                        try
                        {
                            newsbussinessBo.Save();
                        }
                        catch { }
                    }
                }
            }

        }
        #endregion
    }

}